<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/9/21
 * Time: 16:06
 */

namespace app\manage\controller;


class One extends Base
{
    private $onecate;
    private $one;
    public function _initialize() {
        parent::_initialize();
        $this->one= model('one');
        $this->onecate = model('one_cate');
    }

    //单页面列表
    public function index() {

        $tree = new \org\Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $result = $this->onecate->order('ordid')->select();
        $result = objToArray($result);
        $array = array();
        foreach($result as $r) {
            //是否有下一级
            if ($this->onecate->get_child_ids($r['id'])) {
                $r['str_manage'] = '';
            } else {
                $r['str_manage'] = '<a href="'.url('One/edit', ['id'=>$r['id']]).'">编辑</a>';
            }
            $r['parentid_node'] = ($r['pid'])? ' class="child-of-node-'.$r['pid'].'"' : '';
            $array[] = $r;
        }
        $str  = "<tr id='node-\$id' \$parentid_node>
                <td align='center'>\$id</td>
                <td>\$spacer\$name</td>
                <td align='center'>\$str_manage</td>
                </tr>";
        // var_dump($array);
        $tree->init($array);
        $list = $tree->get_tree(0, $str);
        $this->assign('list', $list);
        $this->assign('list_table', true);
       return $this->fetch();
    }

    //单页面编辑
    public function edit() {
        $id = input('param.id/d');
        if($id){
            $cate = $this->onecate->field('id,name,alias')->where('id',$id)->find();
            $info = $this->one->where('cate_id',$id)->find();
            $this->assign('cate',$cate);
            if($info){
                $template = 'edit';
                $this->assign('info',$info);
            }else{
                $template = 'add';
            }
        }else{
            $this->error('参数错误');
        }
        return $this->fetch($template);
    }
    //编辑 前置事件
    public function editDo(){
        \app\manage\model\One::event('before_update',function($obj){
            //自动提取摘要
            if($obj->description == '' && isset($obj->info)) {
                $content = stripslashes($obj->info);
                $obj->description = msubstr(str_replace(array("'","\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($content)),0,200);
                $obj->description = addslashes($obj->description);
            }
        });
        \app\manage\model\One::event('after_update',function($obj){
            action('OneCate/doCache');
        });
        parent::editDo();
    }
    //添加前置事件
    public function addDo(){
        \app\manage\model\One::event('before_insert',function($obj){
            //自动提取摘要
            if($obj->description == '' && isset($obj->info)) {
                $content = stripslashes($obj->info);
                $obj->description = msubstr(str_replace(array("'","\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($content)),0,200);
                $obj->description = addslashes($obj->description);
            }
            $obj->cate_alias = $this->onecate->getCateAlias($obj->cate_id);
        });
        \app\manage\model\One::event('after_insert',function($obj){
            action('OneCate/doCache');
        });
        parent::addDo();
    }
}